
#include <BStyle>
#include <BPainter>
#include <BRender>
#include <BViewer>
#include <BCone>
#include <BCube>
#include <BImage>
#include <BMaterial>
#include <BReadWrite>
#include <BScene>
#include <BCylinder>

#include "HingeHandler.h"

HingeHandler::HingeHandler()
{
	_nodeA = new BBody("nodeA");
	{
		BCube* cube = new BCube(4, 12);
		cube->setColor(100, 200, 255);
		_nodeA->insertShape(cube);
		_nodeA->setMass(0);
	}

	_nodeB = new BBody("debug");
	{
		BCylinder* axisCylinder = new BCylinder(0.5, 2, 36);
		axisCylinder->setColor(200, 100, 0);
		_nodeB->insertShape(axisCylinder);

		BCylinder* bottomFlank = new BCylinder(1, 0.5, 36);
		bottomFlank->setTexture(ReadFile<BImage>("data/picture/Time-For-Lunch-2.jpg"));
		bottomFlank->texture()->setBlend(BTexture::Blend_Modulate);
		bottomFlank->move(0, 0, -1);
		_nodeB->insertShape(bottomFlank);

		BCylinder* topFlank = new BCylinder(1, 0.5, 36);
		topFlank->setTexture(ReadFile<BImage>("data/picture/Time-For-Lunch-2.jpg"));
		topFlank->texture()->setBlend(BTexture::Blend_Modulate);
		topFlank->move(0, 0, 1);
		_nodeB->insertShape(topFlank);

		_nodeB->setMass(1);
	}

	BVector pointA = (_nodeA->space().v5() + _nodeA->space().v6()) / 2 + BVector(1.5, 0, 0);
	BVector axisA(0, 1, 0);
	BVector pointB(0, 0, 0);
	BVector axisB(0, 0, 1);
	_hingeRestraint = new BHingeRestraint(_nodeA, pointA, _nodeB, pointB);
	_hingeRestraint->setName("Hinge");
	_hingeRestraint->setAxisA(axisA);
	_hingeRestraint->setAxisB(axisB);
	_hingeRestraint->setColliding(false);
}
HingeHandler::~HingeHandler()
{

}

void HingeHandler::attached(BViewer* viewer)
{
	BScene* scene = (BScene*)viewer->scene();
	_nodeA->setPosition(0, 0, 5);
	_nodeB->setPosition(2, 2, 5);
	scene->insert(_nodeA);
	scene->insert(_nodeB);
	scene->insert(_hingeRestraint);
	//if (BBody* bodyA = _nodeA->asset())
	//{
	//	BMatrix matrix;
	//	matrix.setPosition(BVector(0, 0, 3));
	//	bodyA->setWorldTransform(matrix);
	//}
	//if (BBody* bodyB = _nodeB->asset())
	//{
	//	BMatrix matrix;
	//	matrix.setPosition(BVector(2, 2, 3));
	//	bodyB->setWorldTransform(matrix);
	//}
	this->emit("edit-restraint", _hingeRestraint);
}
void HingeHandler::detached(BViewer* viewer)
{
	BScene* scene = viewer->scene();
	scene->remove(_nodeA);
	scene->remove(_nodeB);
	scene->remove(_hingeRestraint);
}

void HingeHandler::paint(BPainter& painter)
{
	BViewer* viewer = this->viewer();
}
void HingeHandler::render(BRender& render)
{
	render.setLighting(false);
	render.setDepthTest(false);
	render.setLineSmooth(true);
	render.setLineWidth(1);

	BNode* nodeA = _hingeRestraint->bodyA();
	const BMatrix& matrixA = nodeA->matrix();
	BVector pointA = matrixA * _hingeRestraint->pivotA();
	render.setColor(200, 0, 0);
	render.drawLine(pointA, pointA + _hingeRestraint->horAxis());
	render.setColor(0, 200, 0);
	render.drawLine(pointA, pointA + _hingeRestraint->verAxis());

	BNode* nodeB = _hingeRestraint->bodyB();
	const BMatrix& matrixB = nodeB->matrix();
	render.setColor(250, 250, 0);
	render.drawLine(matrixB.position(), matrixB.position() + _hingeRestraint->swingAxis());
}

void HingeHandler::styleEvent(const BEvent& event)
{
	BHandler::styleEvent(event);
	const BStyle* style = event.value();
	BImage* image = style->image("cone-texture");
	if (BShape* shape = _nodeB->shape(0))
	{
		shape->setTexture(image);
	}
}
